<?php

class ReportController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    }

    public function newAction()
    {
    $tenFile=$_POST['tenFile'];
    if($tenFile!=''){
    	$argv[1]=$tenFile.'.pdf';
    }else{
    	 $argv[1]="Report.pdf";
    }
        if (!isset($argv[1])) {
            echo "Tên File Rỗng\n";
            exit;
        }
        
        try {
            $pdf = Zend_Pdf::load($argv[1]);
        } catch (Zend_Pdf_Exception $e) {
            if ($e->getMessage() == 'Can not open \'' . $argv[1] . '\' file for reading.') {
                // Create new PDF if file doesn't exist
                $pdf = new Zend_Pdf();
        
                if (!isset($argv[2])) {
                    // force complete file rewriting (instead of updating)
                    $argv[2] = $argv[1];
                }
            } else {
                // Throw an exception if it's not the "Can't open file" exception
                throw $e;
            }
        }
        
        //------------------------------------------------------------------------------------
        // Reverse page order
        $pdf->pages = array_reverse($pdf->pages);
        
        // Create new Style
        $style = new Zend_Pdf_Style();
        $style->setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
        $style->setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
        $style->setLineWidth(3);
        $style->setLineDashingPattern(array(3, 2, 3, 4), 1.6);
        $style->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD), 32);
        
        try {
            // Create new image object
        //    require_once 'Zend/Pdf/Image.php';
        //    $stampImage = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/stamp.jpg');
        } catch (Zend_Pdf_Exception $e) {
            // Example of operating with image loading exceptions.
            if ($e->getMessage() != 'Image extension is not installed.' &&
                $e->getMessage() != 'JPG support is not configured properly.') {
                throw $e;
            }
        //    $stampImage = null;
        }
        
        // Mark page as modified
        foreach ($pdf->pages as $page){
            $page->saveGS()
                 ->setAlpha(0.25)
                 ->setStyle($style)
                 ->rotate(0, 0, M_PI_2/3);
        
            $page->saveGS();
            $page->clipCircle(550, -10, 50);
        //    if ($stampImage != null) {
        //        $page->drawImage($stampImage, 500, -60, 600, 40);
        //    }
            $page->restoreGS();
        
            $page->drawText('Modified by Zend Framework!', 150, 0)
                 ->restoreGS();
        }
        
        // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
        $pdf->pages[] = ($page1 = $pdf->newPage('A4'));
        
        // Add new page generated by Zend_Pdf_Page object (page is not attached to the document)
        //$pdf->pages[] = ($page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE));
        
        // Create new font
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        
        // Apply font and draw text
        $page1->setFont($font, 30)
              ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
              ->drawText('Report', 10, 750);
              
        //ve duong doc 
        //	for($i=100;$i<=700;$i+=100){
        //$page1->setLineColor(Zend_Pdf_Color_Html::color('#FF0000'))
        //	  ->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
        //	  ->drawLine($i,200, $i, 700);
        //	}
        
        //ve duong ngang
        //	for($i=200;$i<=700;$i+=50){
        //$page1->setLineColor(Zend_Pdf_Color_Html::color('#000000'))
        //	  ->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
        //	  ->drawLine(100,$i, 500, $i);		
        //	}	 
        
        //ve hinh chu nhat
        //x1,y1,x2,y2 -- wid=x2-x1 --height=y2-y1
        //$page1->setFillColor(new Zend_Pdf_Color_GrayScale(1))
        //      ->setLineColor(Zend_Pdf_Color_Html::color('#000000'))
        //      ->drawRectangle(100, 700, 200, 650); 
        
        //ve nhieu hinh theo duong ngang
        $mang=array('ma san pham','ten san pham','so luong','don gia','thanh tien');
        $m=0;
        for($i=50;$i<=500;$i+=100){
        	$j=$i+100;
        	$k=$i+20;
        	$page1->setFillColor(new Zend_Pdf_Color_GrayScale(1))
              ->setLineColor(Zend_Pdf_Color_Html::color('#000000'))
              ->drawRectangle($i, 700, $j, 650);
              $page1->setFont($font, 10)
              		->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
              		->drawText($mang[$m], $k, 675);
              		$m++;
        }
        
        //ve nhieu hang ngang tiep theo
        
        //ve nhieu duong doc
        //    for($i=700;$i>=450;$i-=50){
        //	$j=$i-50;
        //	$page1->setFillColor(new Zend_Pdf_Color_GrayScale(1))
        //      ->setLineColor(Zend_Pdf_Color_Html::color('#000000'))
        //      ->drawRectangle(100, $i, 200, $j);
        //}   
              
        //------------------------------------------------------------------------------------
        
        if (isset($argv[2])) {
            $pdf->save($argv[2]);
        } else {
            $pdf->save($argv[1], true /* update */);
    }
   }
}



